//
//  SinglePickerView.m
//  PublicCertification
//
//  Created by 邹吕 on 2017/4/24.
//  Copyright © 2017年 zoulv. All rights reserved.
//

#import "SinglePickerView.h"

#define kPickerRowHeight 40

@interface SinglePickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSString      *currentTitle;
    UIView        *_wholeView;
    UIView        *_topView;
    UIPickerView  *_pickerView;
    NSInteger     _comfrom,_index;
}

@property (nonatomic) NSString *titleStr;

@end

@implementation SinglePickerView

-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = YES;
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hiddenBottomView)];
        [self addGestureRecognizer:tap];
        
    }
    return self;
}

-(void)pickViewWithArray:(NSArray *)array andDescArray:(NSArray *)desArray andTitleStr:(NSString *)titleString{
    
    self.titleArray = [array mutableCopy];

    self.desArray = [desArray mutableCopy];
        
    self.titleStr = titleString;
    
    [self createBottomView];
}
-(void)pickViewWithArray:(NSArray *)array andDescArray:(NSArray *)desArray andTitleStr:(NSString *)titleString andComfrom:(NSInteger)comfrom{
    
    self.titleArray = [array mutableCopy];

    self.desArray = [desArray mutableCopy];
        
    self.titleStr = titleString;
    
    [self createBottomView];
    
    _comfrom = comfrom;
    
}
-(void)createBottomView{
    
    // 弹出视图
    _wholeView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 300)];
    [self addSubview:_wholeView];
    _wholeView.backgroundColor = [UIColor whiteColor];
    
    // 头部按钮视图
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    line.backgroundColor = [UIColor colorWithHexString:@"#e2e2e2"];
    [_wholeView addSubview:line];
    
    _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0.5, kScreenWidth, 40)];
    _topView.backgroundColor = [UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1.0];
    [_wholeView addSubview:_topView];
    
    // 防止点击事件触发
    UITapGestureRecognizer *topTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:nil];
    [_topView addGestureRecognizer:topTap];
    
    NSArray *buttonTitleArray = @[@"取消",@"确定"];
    for (int i = 0; i <buttonTitleArray.count ; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i*(kScreenWidth-50), 0, 50, 40);
        [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
        if (i == 0) {
            [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        }else{
            [button setTitleColor:[UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal];
        }
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        [_topView addSubview:button];
        
        button.tag = i;
        [button addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UILabel *titleLab = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, kScreenWidth - 120, 40)];
    titleLab.textColor = [UIColor colorWithHexString:@"#666666"];
    titleLab.font = [UIFont systemFontOfSize:15];
    titleLab.text = self.titleStr;
    titleLab.frame = CGRectMake(60, 0, kScreenWidth - 120, 40);
    titleLab.textAlignment = NSTextAlignmentCenter;
    [_topView addSubview:titleLab];
    
    // pickerView
    _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, kScreenWidth, 260)];
    _pickerView.showsSelectionIndicator=YES;
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
   // [_pickerView selectRow:3 inComponent:0 animated:NO];
    [self pickerView:_pickerView didSelectRow:_selectInteger inComponent:0];
    [_wholeView addSubview:_pickerView];
}

- (void)buttonEvent:(UIButton *)button
{
    // 点击确定回调block
    if (button.tag == 1){

        // 101 支出合同-添加发票专用
        if (_comfrom == 101) {
            if (self.blockIndex) {
                self.blockIndex(_index);
            }
        } else {
            if (_block) {
                _block(currentTitle);
            }
        }
        
        
    }
    
    [self hiddenBottomView];
}

- (void)hiddenBottomView{

    [UIView animateWithDuration:0.3 animations:^{
        
         _wholeView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 300);
         
     } completion:^(BOOL finished) {
         [self removeFromSuperview];
     }];
}

- (void)showBottomView{
    [UIView animateWithDuration:0.3 animations:^{
        
         _wholeView.frame = CGRectMake(0, kScreenHeight-300, kScreenWidth, 300);
         
     } completion:^(BOOL finished) {}];
    
}


#pragma mark - UIPickerViewDelegate

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)componen{
    return self.titleArray.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    return [self.titleArray objectAtIndex:row];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    if (!view) {
        view = [[UIView alloc]init];
    }

    
    UILabel *lab1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth,kPickerRowHeight)];
   
    if (self.desArray == nil) {
        lab1.text = [NSString stringWithFormat:@"%@",self.titleArray[row]];
    }else{
        lab1.text = [NSString stringWithFormat:@"%@%@",self.titleArray[row],self.desArray[row]];
    }
    
    lab1.font = [UIFont systemFontOfSize:16];
    lab1.textAlignment = NSTextAlignmentCenter;
    lab1.textColor =[UIColor blackColor];
    [view addSubview:lab1];
    
//    [lab1 addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:12]} forString:self.desArray[row]];
    
    return view;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [pickerView selectRow:row inComponent:component animated:YES];
    if (ObjectIsEmpty(self.titleArray)) {
          return;
      }
    currentTitle = self.titleArray[row];
    _index = row;
}


-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return kPickerRowHeight;
}

@end
